'use strict';

let nunjucks = require('nunjucks');
// nunjucks configure File
function createEnv(path, opts) {
    path = path || 'views';
    opts = opts || {};
    let confOpts = {
        autoescape: opts.autoescape || true,
        throwOnundefined: opts.throwOnundefined || false,
        trimBlocks: opts.trimBlocks || true,
        isTrimBlocks: opts.isTrimBlocks || true,
        watch: opts.watch || true,
        noCache: opts.noCache || true
    }
    let env = nunjucks.configure(path, [opts]);
    return env;
};

// nunjucks MiddleWare File
let MiddleWare = async (ctx, next) => {
    ctx.render = (view, model) => {
        let env = createEnv();
        ctx.body = env.render(view, model);
    }
    await next();
};

module.exports = MiddleWare;